package com.hckj.jianzhijia.fragments;

import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.avoscloud.leanchatlib.activity.AVChatActivity;
import com.example.administrator.floatingbutton.FloatingActionButton;
import com.hckj.jianzhijia.PublishActivity;
import com.hckj.jianzhijia.R;
import com.hckj.jianzhijia.WebPageActivity;
import com.hckj.jianzhijia.adapters.BDFragmentPagerAdapter;
import com.hckj.jianzhijia.chat.ContactFragment;
import com.hckj.jianzhijia.chat.ConversationFragment;
import com.hckj.jianzhijia.chat.ConversationFragmentUpdateEvent;
import com.hckj.jianzhijia.chat.PersonalProfileFragment;
import com.rey.material.widget.Button;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;

public class MainChatFragment extends Fragment {


    private Button btn_approving_jobs;
    private Button btn_published_jobs;
    private Button btn_finished_jobs;
    private RelativeLayout rl_parent;

    private ConversationFragment approvingJJobFragment;
    private ContactFragment publishedJobFragment;
    private PersonalProfileFragment finishedJobFragment;
    private ArrayList<Fragment> fragmentArrayList = null;

    private ImageView imgv_line;
    private ViewPager vp_fragment;
    private int line_offset = 0;
    private int line_currIndex = 0;
    private int line_bmpW;



    public static MainChatFragment newInstance() {
        MainChatFragment fragment = new MainChatFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main_chat, container, false);
        this.btn_approving_jobs = (Button) view.findViewById(R.id.btn_approving_jobs);
        this.btn_finished_jobs = (Button) view.findViewById(R.id.btn_finished_jobs);
        this.btn_published_jobs = (Button) view.findViewById(R.id.btn_published_jobs);
        rl_parent = (RelativeLayout) view.findViewById(R.id.rl_parent);
        rl_parent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), AVChatActivity.class));
            }
        });
        imgv_line = (ImageView) view.findViewById(R.id.imgv_line);
        vp_fragment = (ViewPager) view.findViewById(R.id.vp_fragment);
        rl_parent = (RelativeLayout) view.findViewById(R.id.rl_parent);
        initFragment();
        initPagerBottomLine();
        initEvent();
        return view;
    }



    //事件初始化
    private void initEvent() {

        BDFragmentPagerAdapter  bdFragmentPagerAdapter = new BDFragmentPagerAdapter(getFragmentManager(),fragmentArrayList);
        vp_fragment.setOffscreenPageLimit(3);
        vp_fragment.setCurrentItem(0);
        vp_fragment.setAdapter(bdFragmentPagerAdapter);
        vp_fragment.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            int one = line_offset * 2 + line_bmpW;// 页卡1 -> 页卡2 偏移量

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {


                Animation animation = new TranslateAnimation(one * line_currIndex, one * position, 0, 0);
                line_currIndex = position;
                animation.setFillAfter(true);// True:图片停在动画结束位置
                animation.setDuration(150);
                imgv_line.startAnimation(animation);
                switch (position) {
                    case 0:
                        btn_approving_jobs.setTextColor(getResources().getColor(R.color.orange_FF6700_btn));
                        btn_published_jobs.setTextColor(getResources().getColor(R.color.nomal_text_color));
                        btn_finished_jobs.setTextColor(getResources().getColor(R.color.nomal_text_color));
                        break;
                    case 1:
                        btn_approving_jobs.setTextColor(getResources().getColor(R.color.nomal_text_color));
                        btn_published_jobs.setTextColor(getResources().getColor(R.color.orange_FF6700_btn));
                        btn_finished_jobs.setTextColor(getResources().getColor(R.color.nomal_text_color));
                        break;
                    case 2:
                        btn_approving_jobs.setTextColor(getResources().getColor(R.color.nomal_text_color));
                        btn_published_jobs.setTextColor(getResources().getColor(R.color.nomal_text_color));
                        btn_finished_jobs.setTextColor(getResources().getColor(R.color.orange_FF6700_btn));
                        break;
                }
                if (0 == position) {
                    EventBus.getDefault().post(new ConversationFragmentUpdateEvent());
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.btn_approving_jobs:
                        vp_fragment.setCurrentItem(0);
                        break;
                    case R.id.btn_published_jobs:
                        vp_fragment.setCurrentItem(1);
                        break;
                    case R.id.btn_finished_jobs:
                        vp_fragment.setCurrentItem(2);
                        break;
                }

            }
        };
        btn_approving_jobs.setOnClickListener(onClickListener);
        btn_published_jobs.setOnClickListener(onClickListener);
        btn_finished_jobs.setOnClickListener(onClickListener);
    }

    //初始化滑动线条
    private void initPagerBottomLine() {

        line_bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.nicon_bottom_line).getWidth();
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenW = dm.widthPixels;// 获取分辨率宽度
        line_offset = (screenW / 3 - line_bmpW) / 2;// 计算偏移量
        Matrix matrix = new Matrix();
        matrix.postTranslate(line_offset, 0);
        imgv_line.setImageMatrix(matrix);

    }

    //初始化fragment
    private void initFragment() {

        approvingJJobFragment = new ConversationFragment();
        publishedJobFragment = new ContactFragment();
        finishedJobFragment = new PersonalProfileFragment();
        fragmentArrayList = new ArrayList<Fragment>();
        fragmentArrayList.add(approvingJJobFragment);
        fragmentArrayList.add(publishedJobFragment);
        fragmentArrayList.add(finishedJobFragment);
    }
}
